//
//  DevelopAssociateViewController.swift
//  HuaYun
//
//  Created by enghou on 2019/1/15.
//  Copyright © 2019 xyxorigation. All rights reserved.
//

import UIKit

class DevelopAssociateViewController: IncomeCostProjects {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.ratio.inc()
        ratio.titleLabel.text = "完成率"
        self.tableView.separatorInset = .zero
        self.tableView.register(UINib.init(nibName: "DevelopDetailTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "DevelopDetailTableViewCell")
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return self.tableView.dequeueReusableCell(withIdentifier: "DevelopDetailTableViewCell")!
    }
    
    let ratio = Bundle.main.loadNibNamed("RatioView", owner: nil, options: nil)![0] as! RatioView
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 54
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let view = UIView()
        
        let top = UIView()
        top.backgroundColor = UIColor.dftBack
        view.addSubview(top)
        top.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.width.equalToSuperview()
            make.height.equalTo(10)
            make.top.equalToSuperview()
        }
        
        let stack = UIStackView.init()
        view.addSubview(stack)
        stack.alignment = .fill
        stack.distribution = .fillEqually
        stack.spacing = 1
        
        let labels = ["成本预算","成本执行"]
        for i in 0 ..< 2 {
            let label = UILabel()
            label.text = labels[i]
            label.backgroundColor = UIColor.white
            label.textAlignment = .center
            label.font = UIFont.systemFont(ofSize: 14)
            stack.addArrangedSubview(label)
        }
        
        stack.addArrangedSubview(ratio)
        stack.snp.makeConstraints { (make) in
            make.top.equalTo(10)
            make.width.equalToSuperview()
            make.bottom.equalToSuperview()
            make.left.equalToSuperview()
        }
        
        view.backgroundColor = UIColor.tableSeparator
        
        ratio.button.addTarget(self.field, action: #selector(becomeFirstResponder), for: .touchUpInside)
        
        let line = UIView()
        line.backgroundColor = UIColor.tableSeparator
        view.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(0.5)
            make.bottom.equalToSuperview()
        }
        
        return view
    }
    
    
    
    override func endedit() {
        let index = self.picker.selectedRow(inComponent: 0)
        if index == 0 {
            //升序
            self.ratio.inc()
            NotificationCenter.default.post(name: NSNotification.Name("RefreshDetail"), object: 1)
        } else {
            //降
            self.ratio.dec()
            NotificationCenter.default.post(name: NSNotification.Name("RefreshDetail"), object: 2)
        }
        self.view.endEditing(true)
    }
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if let cel = cell as? DevelopDetailTableViewCell {
            cel.model = self.source[indexPath.row]
        }
    }
    

}
